<style>
.NewsTdHeight{ line-height:20px; }
</style>
<fieldset class="cbi-section">
	<table width="100%" class="NewsTdHeight">
		<tr><td width="35%" align="right"><%:Select the device model:%></td>
			<td  width="65%" align="left">
				<select name="amlogic_soc" id="amlogic_soc" style="width:auto" onchange="sel_dtb_input(this.options[this.selectedIndex].value)">
				<option value="0"><%:Select List%></option>
				<option value="11"><%:[s905d] Phicomm N1%></option>
				<option value="12"><%:[s905d] Phicomm N1 (DMA thresh)%></option>
				<option value="13"><%:[s905x] HG680P & B860H%></option>
				<option value="14"><%:[s905w] X96-Mini & TX3-Mini%></option>
				<option value="21"><%:[s912] Octopus Planet%></option>
				<option value="31"><%:[s905x2] X96 Max 4G%></option>
				<option value="32"><%:[s905x2] X96 Max 2G%></option>
				<option value="41"><%:[s922x] Belink GT-King%></option>
				<option value="42"><%:[s922x] Belink GT-King Pro%></option>
				<option value="43"><%:[s922x] Belink GT-King Pro H%></option>
				<option value="44"><%:[s922x] Belink GT-King Pro Rev_A%></option>
				<option value="45"><%:[s922x] Hardkernel ODROID N2%></option>
				<option value="46"><%:[s922x] UGOOS AM6 Plus%></option>
				<option value="47"><%:[a311d] Khadas VIM3%></option>
				<option value="51"><%:[s905x3] X96 Max+%></option>
				<option value="52"><%:[s905x3] X96 Max+ (OverClock@2208Mhz)%></option>
				<option value="53"><%:[s905x3] HK1 Box%></option>
				<option value="54"><%:[s905x3] HK1 Box (OverClock@2208Mhz)%></option>
				<option value="55"><%:[s905x3] H96 Max X3%></option>
				<option value="56"><%:[s905x3] H96 Max X3 (OverClock@2208Mhz)%></option>
				<option value="57"><%:[s905x3] Ugoos X3%></option>
				<option value="58"><%:[s905x3] Ugoos X3 (OverClock@2208Mhz)%></option>
				<option value="59"><%:[s905x3] TX3 BZ%></option>
				<option value="5a"><%:[s905x3] TX3 BZ (OverClock@2208Mhz)%></option>
				<option value="5b"><%:[s905x3] TX3 QZ%></option>
				<option value="5c"><%:[s905x3] TX3 QZ (OverClock@2208Mhz)%></option>
				<option value="99"><%:Enter the dtb file name%></option>
				</select>
			</td>
		</tr>
		<tr id="tr_sel_dtb" style="display:none;"><td width="30%" align="right"><%:Or enter the dtb file name:%></td><td  width="70%" align="left"><input class="cbi-input-file" style="width: 235px" type="text" id="amlogic_dtb" name="amlogic_dtb" /></td></tr>
		<tr>
			<td width="30%" align="right"><%:Install OpenWrt:%></td>
			<td  width="70%" align="left">
				<input type="button" class="cbi-button cbi-button-reload" value="<%:Install%>" onclick="return amlogic_install(this, '<%=self:cfgvalue(section)%>')"/>　<span id="_check_install"></span>
			</td>
		</tr>
	</table>
</fieldset>
<script type="text/javascript">//<![CDATA[

	// Show custom dtb input box
	function sel_dtb_input(soc_value){
		if ( soc_value == "99" )
		{
			document.getElementById('tr_sel_dtb').style = '';
		}
		else
		{
			document.getElementById('tr_sel_dtb').style = 'display:none;';
		}
	}

	// Show the status of the install button
	function amlogic_install(btn,amlogic_install_sel)
	{
		var amlogic_soc_id = document.getElementById('amlogic_soc');
		var amlogic_soc_index = amlogic_soc_id.selectedIndex;
		var amlogic_soc_value = amlogic_soc_id.options[amlogic_soc_index].value;
		var amlogic_soc_text = amlogic_soc_id.options[amlogic_soc_index].text;
		var amlogic_confirm_info = amlogic_soc_text + "@" + amlogic_soc_value;

		var amlogic_dtb_id = document.getElementById('amlogic_dtb');
		var amlogic_dtb_value = amlogic_dtb_id.value;

		if (confirm('<%:You have chosen:%> [ ' + amlogic_confirm_info + ' ] , <%:Start install?%>') != true) { return false; }

		btn.disabled = true;
		btn.value    = '<%:Installing...%> ';

		XHR.get('<%=luci.dispatcher.build_url("admin", "system", "amlogic", "start_amlogic_install")%>',
			{
				amlogic_install_sel: amlogic_soc_value + '@' + amlogic_dtb_value
			},
			function(x,status)
			{
				if ( x && x.status == 200 ) {
					if(status.rule_install_status=="1")
					{
						btn.value = '<%:Install Failed%>';
					}
					else
					{
						btn.value = '<%:Successful Install%>';
					}
				}
				else {
					btn.value = '<%:Install%>';
				}
			}
		);
		btn.disabled = false;
		return false;
	}

	// Read openwrt install log
	var start_check_install = document.getElementById('_check_install');
	XHR.poll(1, '<%=luci.dispatcher.build_url("admin", "system", "amlogic", "start_check_install")%>', status.start_check_install, function(x, status) {
	if ( x && x.status == 200 ) {
		if ( status.start_check_install != "\n" && status.start_check_install != "" ) {
			start_check_install.innerHTML = '<font color="blue"> '+status.start_check_install+'</font>';
		}
		if ( status.start_check_install == "\n" || status.start_check_install == "" ) {
			start_check_install.innerHTML = '';
		}
	}
	});

	// Show external modal database: /etc/model_database.txt
	mymodel_arrlen = 0;
	_option_codes = "";
	var _check_my_amlogic_soc = document.getElementById('amlogic_soc');
	XHR.get('<%=luci.dispatcher.build_url("admin", "system", "amlogic", "start_model_database")%>', null, function(x, status) {
		if ( x && x.status == 200 ) {
			if ( status.my_model_database != "" ) {
				_option_codes = _option_codes + '<option value="0"><%:Select List%></option>';
				let userModelList = status.my_model_database;
				var user_arry = userModelList.split('@@@');
				var mymodel_arrlen = user_arry.length - 1;
				for ( j = 0; j < mymodel_arrlen; j++ ) {
					my_box_mode = user_arry[j].split('###');
					my_box_mode_id = my_box_mode[0];
					my_box_mode_name = my_box_mode[1];
					if ( parseFloat(my_box_mode_id).toString() != "NaN" && my_box_mode_name != "" ) {
						_option_codes = _option_codes + '<option value="' + my_box_mode_id + '">' + my_box_mode_name.replace(/~/g, " ") + '</option>';
					}
				}
				_option_codes = _option_codes + '<option value="99"><%:Enter the dtb file name%></option>';
				_check_my_amlogic_soc.innerHTML = _option_codes;
			}
		}
	});

//]]></script>
